<?php
class UploadFoja extends Controller
{
	function UploadFoja()
	{
		parent::Controller();
		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');
		$this->load->model('causas_model');
		$this->load->library('my_library');
	}
	
	function index()
	{
		$this->load->view('mod_causas/uploadFoja',array('error' => ' ' ));	
	}
	
function uploadFile()
	{		
		$config = array(
		array(
			'field'	=> 	'rolUpfile',
			'label'	=> 	'Rol',
			'rules'	=> 	'required'
		
		),
		array(
			'field' => 	'fechaUpload',
			'label' =>	'Fecha',
			'rules'	=>	'required'	
		),
		array(
			'field' => 	'actuacion',
			'label' =>	'Tipo Actuación',
			'rules'	=>	'required'	
		));
		
		$this->form_validation->set_rules($config);
		$upload_path = '';
		if ($this->form_validation->run() == FALSE)
		{

			echo validation_errors('<div class="msj_error">','</div>');
			
		}
		else
		{
			if(!empty($_FILES['userfile']['name']))
			{
				$config['upload_path'] = './causas/';
				//$hist = $this->causas_model->isHistorial($this->input->post('rolUpfile'));
				$config['allowed_types'] = 'pdf|jpg|png|gif';

				//$config['max_width'] = 300;
				//$config['max_height'] = 400;
				$this->load->library('upload', $config);
			
				if ( ! $this->upload->do_upload())
				{
					echo $this->upload->display_errors('<div class="msj_error">','</div>');
					
				}	
				else
				{
					$data = array('upload_data' => $this->upload->data());
					$file_name = $data['upload_data']['file_name'];
					$file_type = $data['upload_data']['is_image'];
					$upload_path = './causas/'.$file_name;
					if($file_type == 1)
					{
						$config['image_library'] = 'gd2';
						$config['source_image'] = $upload_path;
						$config['create_thumb'] = FALSE;
						$config['maintain_ratio'] = FALSE;
						$config['width'] = 600;
						$config['height'] = 700;
						
						$this->load->library('image_lib', $config);
						
						$this->image_lib->resize();
					}
					$this->followCausa($upload_path, $file_type);									

				}
			}
			else
			{
				$file_type = '';
				$upload_path ='';
				$this->followCausa($upload_path,$file_type);
			}
		}
	}
	function followCausa($upload_path, $file_type) {
		$ord = $this->causas_model->getLastFoja($this->input->post('rolUpfile'));
		if(empty($ord))
			$orden_foja = 0;
		else
			$orden_foja = $ord->orden_foja + 1;
		
		$this->load->helper('date');
		$datestring = "%H:%i:%s";
		$time = time();

		$hora = mdate($datestring, $time);
		$fecha = $this->input->post('fechaUpload').' '.$hora;
		$data = array(
			'causa_rol' =>	$this->input->post('rolUpfile'),
			'fecha' => $fecha,
			'tipo_actuacion' => $this->input->post('actuacion'),
			'ruta_archivo' => $upload_path,
			'tipo' => $file_type,
			'orden_foja' => $orden_foja
		);
		
		if($this->causas_model->storeFoja($data))
		{
			echo "<div class='msj_success'>El registro a sido almacenado exitosamente.</div>";
			if($file_type == 1){	
				$this->createXML($this->input->post('rolUpfile'));
				echo "<div class='msj_success'>Puede mirar la causa en papel digital haciendo click <a href='".base_url()."index.php/mod_causas/causas/showFoja/".$this->input->post('rolUpfile')."' target=_blank>aquí</a>.</div>";
			}
		}
		else
		{
			echo "<div class='msj_error'>Error al subir archivo.</div>";
		}

	}

	function delete_foja()
	{
		$id_foja = $this->uri->segment(4);
		$idCausa = $this->causas_model->get_id_causa_foja($id_foja);	
		if($this->causas_model->delete_foja($id_foja))
		{
		    $data['results'] = $this->causas_model->getCausaRol($idCausa);
		    $data['history'] = $this->causas_model->getHistory($idCausa);
		    if($data['history'] -> num_rows() == 0)
		    {
		    	$data['cols'] = 0;
		    	$this->load->view('mod_causas/showCausasDetails',$data);
		    }
		    else
		    {
		    	$data['cols'] = 1;
		    	$this->load->view('mod_causas/showCausasDetails',$data);
		    }
		}
		else
		{
			echo "<div class='msj_error'>Error al eliminar foja, presione F5 he intente nuevamente.</div>";
		}
	}
	
	function createXML($idCausa){
		$this->load->helper('file');
		$width_img = 600;
		$height_img = 700;
		$flag = 0;
		$preload  = 'true';
		$xml = '<content width="'.$width_img.'" height="'.$height_img.'" hcover="false" transparency="true" >
	';
		//Obtenemos los archivos
		$files = $this->causas_model->getFilesCausa($idCausa, 'asc', '1');
		foreach($files->result() as $file):
			$xml .= '<page src="'.base_url().''.$file->ruta_archivo.'" preLoad="'.$preload.'" />
	';
			$preload = 'false';
		endforeach;
		$xml .= '
</content>';
		
		write_file('causas/file_xml/'.$idCausa.'.xml', $xml);		
	}
}
?>